package com.aivin.waterfallflow;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.aivin.waterfallflow.adapter.PicAdapter;
import com.aivin.waterfallflow.bean.MarginDecorationForStaggered;
import com.aivin.waterfallflow.bean.PicBean;
import com.aivin.waterfallflow.callback.IGetOneShareSpiderCallBack;
import com.aivin.waterfallflow.callback.PicLoadProgressCallback;
import com.aivin.waterfallflow.callback.StaggeredGridLayoutManagerEndlessRecyclerOnScrollListener;
import com.aivin.waterfallflow.tool.ListPageUtil;
import com.aivin.waterfallflow.tool.MyDeviceInforHelper;
import com.aivin.waterfallflow.tool.SpiderOneShareDataTool;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;


public class PicShowActivity extends BaseActivity {
    RecyclerView recyclerView;
    private Context context ;
    private SpiderOneShareDataTool spiderOneShareDataTool = new SpiderOneShareDataTool();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.lib_media_activity_picshow);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        context = this ;

        initView();
        getPics();
    }

    private void  getPics(){
        spiderOneShareDataTool.getOneShareData("https://gitee.com/hnyer/workpics/raw/master/%E8%BD%BB%E6%9D%BE%E4%B8%80%E5%88%BB1_json.md" ,iGetOneShareSpiderCallBack ,2);
    }

    private ListPageUtil listPageUtil ;
    private IGetOneShareSpiderCallBack iGetOneShareSpiderCallBack =
            new IGetOneShareSpiderCallBack() {
                @Override
                public void onGetData(String jsonStr , int type) {

                    List<String> picPaths = JSONObject.parseArray(jsonStr, String.class);
                    listPageUtil = new ListPageUtil(picPaths, 30);  // 每次加载30条


                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            boolean hasNex =listPageUtil.hasNext() ;
                            if(!hasNex){
                                // 如果已经加载完成
                                adapter.setHasMoreData(false);
                                return;
                            }
                            List<PicBean> pageData =  getNewData () ;// listPageUtil.nextPage();
                            adapter.updateData(pageData); //  init posst
                            adapter.setHasMoreData(true);
                        }
                    });
                }

                @Override
                public void onStartLoad() {

                }

                @Override
                public void onError(String error) {

                }
            };



    private PicAdapter adapter ;
    private void initView(){
        recyclerView = findViewById(R.id.recyclerviewPics) ;

        recyclerView.setHasFixedSize(true);
        // 瀑布流分n列显示
        int columnCount = 3;
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(columnCount,StaggeredGridLayoutManager.VERTICAL) ;


        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MarginDecorationForStaggered(context ,columnCount , MyDeviceInforHelper.dp2px(context ,2)));

        recyclerView.addOnScrollListener(new StaggeredGridLayoutManagerEndlessRecyclerOnScrollListener() {
            @Override
            public void onLoadMore() {
                if(listPageUtil ==null){
                    return;
                }

                boolean hasNex =listPageUtil.hasNext() ;
                if(!hasNex){
                    // 如果已经加载完成
                    adapter.setHasMoreData(false);
                    return;
                }
                List<PicBean> pageData =  getNewData () ;

                adapter.updateData(pageData); // onLoadMore
                adapter.setHasMoreData(true);
            }
        });

        List<PicBean> datas = new ArrayList<>() ;
        adapter = new PicAdapter(  datas, columnCount ,new PicAdapter.PicSelectCallBack() {
            @Override
            public void setSelectedCallBack(int index) {

            }
        } ,picLoadProgressCallback);
        recyclerView.setAdapter(adapter);
    }


    private PicLoadProgressCallback picLoadProgressCallback = new PicLoadProgressCallback() {
        @Override
        public void onStartLoad() {
            showLoadDialog();
        }

        @Override
        public void onLoading(String msg) {
            setDialogLoadingINfo(msg);
        }

        @Override
        public void onFinishLoad() {
            hideLoadDialog();
        }
    } ;


    private List<PicBean> getNewData(){
        List<PicBean> pageData = new ArrayList<>();
        List<String> temp = listPageUtil.nextPage();
        if(temp==null){
            return null ;
        }
        for(String str: temp){
            pageData.add(new PicBean(str));
        }
        return pageData ;
    }

}